import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class demo01 {
    public static void main(String[] args) throws InterruptedException, IOException {
//        test1();

//        test2();

//        test3();

//        test4();

//        page1();

//        page2();

//        page3();

//        page4();

//        page5();

//        test5();

//        test6();

        test7();
    }
    private static void test1(){
        //创建驱动
        WebDriver webDriver=new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/.");
        //判断元素是否可以被点击
        WebDriverWait wait=new WebDriverWait(webDriver,1);
//        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bottom_layer")));
        wait.until(ExpectedConditions.titleIs("百度一下，你就知道"));
    }

    private static void test2() throws InterruptedException {
        WebDriver webDriver=new ChromeDriver();
        //打开百度
        webDriver.get("https://www.baidu.com/");
        //搜索521
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        //浏览器后退
        webDriver.navigate().back();
        sleep(3000);
        //浏览器刷新
        webDriver.navigate().refresh();
        sleep(3000);
        //浏览器前进
        webDriver.navigate().forward();
        sleep(3000);

        ((JavascriptExecutor)webDriver).executeScript("ocument.documentElement.scrollTop=10000");
        webDriver.manage().window().maximize();
        sleep(3000);
        webDriver.manage().window().fullscreen();
        sleep(3000);
        webDriver.manage().window().setSize(new Dimension(600,1000));
    }

    private static void test3() throws InterruptedException {
        WebDriver webDriver=new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/");
        //搜索521
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
        //control+A
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"A");
        sleep(3000);
        //control+X
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"X");
        sleep(3000);
        //control+V
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"V");
        sleep(3000);

        webDriver.findElement(By.cssSelector("#su")).click();
    }

    public static void test4() throws InterruptedException {
        WebDriver webDriver=new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("520");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        //找到图片的按钮
        WebElement webElement=webDriver.findElement(By.cssSelector("#s_tab > div > a.s-tab-item.s-tab-item_1CwH-.s-tab-pic_p4Uej.s-tab-pic"));
        //鼠标右击
        Actions actions=new Actions(webDriver);
        sleep(3000);
        actions.moveToElement(webElement).contextClick().perform();
    }

    private static void page1() {
        WebDriver webDriver=new ChromeDriver();
        webDriver.get("http://localhost:63342/_20230512testcode/src/main/Page/test01.html?_ijt=4brheilpuaaaoa465u843m3sth&_ij_reload=RELOAD_ON_SAVE");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);
        List<WebElement> webElements=webDriver.findElements(By.cssSelector("input"));
        for (int i = 0; i < webElements.size(); i++) {
            //getAttribute获取页面上的元素属性值，里面的type是当前元素属性
            if(webElements.get(i).getAttribute("type").equals("checkbox")){
                webElements.get(i).click();
            }else{
                ;
            }
        }
    }

    private static void page2(){
        WebDriver webDriver=new ChromeDriver();
        webDriver.get("http://localhost:63342/_20230512testcode/src/main/Page/test02.html?_ijt=arpr09o5r3gegeidj4o2r6hc9b&_ij_reload=RELOAD_ON_SAVE");
        webDriver.switchTo().frame("f1");
        webDriver.findElement(By.cssSelector("body > div > div > a")).click();
    }

    private static void page3(){
        WebDriver webDriver=new ChromeDriver();
        webDriver.get("http://localhost:63342/_20230512testcode/src/main/Page/test03.html?_ijt=qje1h6c6groepeefv0ae9bnla2&_ij_reload=RELOAD_ON_SAVE");
        WebElement webElement=webDriver.findElement(By.cssSelector("#ShippingMethod"));
        Select select=new Select(webElement);
//        select.selectByIndex(3);
        select.selectByValue("12.51");
    }

    private static void page4() throws InterruptedException {
        WebDriver webDriver=new ChromeDriver();
        webDriver.get("http://localhost:63342/_20230512testcode/src/main/Page/test04.html?_ijt=u8fai3bvc2dvsjigccqcapieg1&_ij_reload=RELOAD_ON_SAVE");
        webDriver.findElement(By.cssSelector("button")).click();
        sleep(3000);
        //alert弹窗取消
        webDriver.switchTo().alert().dismiss();
        sleep(3000);
        //点击按钮
        webDriver.findElement(By.cssSelector("button")).click();
        //在alert弹窗中输入呵呵
        webDriver.switchTo().alert().sendKeys("呵呵");
        //alert弹窗确认
        sleep(3000);
        webDriver.switchTo().alert().accept();
    }

    private static void page5(){
        WebDriver webDriver=new ChromeDriver();
        webDriver.get("http://localhost:63342/_20230512testcode/src/main/Page/test05.html?_ijt=7asvni2vctahrdl90k8v4lvq5&_ij_reload=RELOAD_ON_SAVE");
        webDriver.findElement(By.cssSelector("input")).sendKeys("D:\\untitled");
    }

    //弹窗关闭
    private static void test5() throws InterruptedException {
        WebDriver webDriver=new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        sleep(3000);
//        webDriver.quit();
        webDriver.close();
    }


    private static void test6() throws InterruptedException {
        WebDriver webDriver=new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        sleep(3000);
        //通过getWindowHandles获取所有的窗口句柄
        //通过getWindowHandle获取get打开的页面窗口句柄
        System.out.println(webDriver.getWindowHandle());
        Set<String> handles=webDriver.getWindowHandles();
        String target_handle="";
        for (String handle:handles) {
            target_handle=handle;
        }
        webDriver.switchTo().window(target_handle);
        sleep(3000);
        webDriver.findElement(By.cssSelector("#ww")).sendKeys("新闻联播");
        webDriver.findElement(By.cssSelector("#s_btn_wr")).click();
    }

    //截屏保存图片
    public static void test7() throws InterruptedException, IOException {
        WebDriver webDriver=new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        File file=((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file,new File("D://111.png"));
    }
}
